﻿    using System;
    namespace Putto 
    {     
            class Program
            {
           	 public void Swap(ref int valOne, ref int valTwo)
    			{
      			 valOne = valOne + valTwo;
       			 valTwo = valOne - valTwo;
       			 valOne = valOne - valTwo;
    			}
             public void Sorsol(ref byte limit, ref int fieldvalue)
            	{
            	Random rng = new Random();
            	fieldvalue = rng.Next(1, limit);
            	}
             public void Message(string message, bool newline)
             {
             	if(newline)
             		Console.WriteLine(message);
             	else
             		Console.Write(message);
             }
                    public static void Main(string[] args)
                    {              
                    	Console.Title = "Puttó";
                    		//string format = "";
    						int profit = 0;
                            uint games = 1;
                            bool doingSimulation = false;
                            int bet = 0;
                            int netGain = 0;
                            int multiplier = 0;
                            uint i = 0;
                            int[] stats = new int[10];
                            for(int z = 0; z < 9; z++)
                            {
                            stats[z] = 0;
                            }
                            uint all = 0;
                            bool playing = false;                   
start:         
                            Console.Clear();
                            Program Putto = new Program();
                        	byte MINRANGE = 1; //Used by RNG -> +1
                        	byte MAXRANGE = 21; //Used by RNG -> +1
                            byte MAXRANGE_B = 5; //Used by RNG -> +1
                            bool b = false;
                        	byte hits = 0;
                            int[] tip = new int[7];
                            int arraylength = 0;
                            int[] scored = new int[7];
                            int nB = 0;
                            int nowB = 0;
                            if(playing && !doingSimulation)
                            {
                            Putto.Message("Adj meg egy tétet, amivel játszani szeretnél! 200-400-600-800-1000", true);
                            bet = int.Parse(Console.ReadLine());
                            while(bet != 200 && bet != 400 && bet != 600 && bet != 800  && bet != 1000)
                            {
                            Putto.Message("A tét csak 200-400-600-800-1000 Forint lehet. Adj meg érvényes tétet!", true);
                            bet = int.Parse(Console.ReadLine());
                            }
                            profit -= bet;
                            }
                            Console.Error = "asd";
                            Putto.Message("Add meg a számokat amikkel játszani szeretnél, enterrel elválasztva!", true);
                            for(i = 0; i<7;)
                            {  	i++;
                            	try
                            	{
                            	tip[i] = int.Parse(Console.ReadLine());
                            	while(tip[i] < MINRANGE || tip[i] > MAXRANGE)
                            		{
                            			Putto.Message("Az A mező számai csak 1 és 20 között lehetnek!", true);
                            			tip[i] = int.Parse(Console.ReadLine());
                            		}
                            	
                            	for(int j = 0; j < arraylength; j++)
                            	{
                            		while(tip[i] == tip[j])
                            		{
                            		Putto.Message("Ezt a számot már választottad!", true);
                            		tip[i] = int.Parse(Console.ReadLine());
                            		j = 0;
                            		}
                            	}
                            arraylength ++;
                            }
                            
                            catch
                            {
                            	Putto.Message("Számot kell megadnod!", true);
                            	i--;
                            }
                            }
                            Console.WriteLine("B mező?");
                            nB = byte.Parse(Console.ReadLine());
                            while(nB > 4 || nB < 1)
                            {
                            Console.WriteLine("A B mezőben csak 1 és 4 közötti szám szerepelhet! válassz másikat!");
                            nB = byte.Parse(Console.ReadLine());
                            }
    //Sorsolás----------------------------------------------
    SimBelep:
                            hits = 0;
                            arraylength = 0;
                            for(i = 0; i<7; i++)
                            {
                            	Putto.Sorsol(ref MAXRANGE, ref scored[i]);
                            	for(int j = 0; j < arraylength; j++)
                            	{
                            		while(scored[i] == scored[j])
                            		{
                            		Putto.Sorsol(ref MAXRANGE, ref scored[i]);
                            		j = 0;
                            		}
                            	}
                            arraylength ++;
                            }
                            Putto.Sorsol(ref MAXRANGE_B, ref nowB);
                           
    //---------[Sorbarendezés]----------------------------------------------
    int[] inputArray = new int[7];
    for(i = 0; i<7; i++)
    {
    	inputArray[i] = scored[i];
    }
     
    for (int iterator = 0; iterator < inputArray.Length; iterator++)
        {
            for (int index = 0; index < inputArray.Length - 1; index++)
            {
                if (inputArray[index] > inputArray[index + 1])
                {
                    Putto.Swap(ref inputArray[index], ref inputArray[index+1]);
                }
            }
        }  

    for(i = 0; i<7; i++)
    	scored[i] = inputArray[i];
    for(i = 0; i<7; i++)
    	inputArray[i] = tip[i];

     
    for (int iterator = 0; iterator < inputArray.Length; iterator++)
        {
            for (int index = 0; index < inputArray.Length - 1; index++)
            {
                if (inputArray[index] > inputArray[index + 1])
                {
                    Putto.Swap(ref inputArray[index], ref inputArray[index+1]);
                }
            }
        }  
    for(i = 0; i<7; i++)
    	tip[i] = inputArray[i];
                            Console.Clear();
                            hits = 0;
                            netGain = 0;
                            multiplier = 0;
                            Console.WriteLine("Számaid: ");
                            for(i = 0; i < 7; i ++)
                            	Console.Write(tip[i]+ " ");
                            Console.WriteLine(" B mező: {0}", nB);
                            Console.Write("Sorsolás: ");
                            for(i = 0; i < 7; i++)
                            	Console.Write(scored[i] + " ");
                            Console.WriteLine(" B mező: {0}", nowB);
                           for(i = 0; i < 7; i ++)
                           {
                           	for(int j = 0; j < 7; j++)
                           	{
                           		if(tip[i] == scored[j])
                           		   hits++;
                           	}
                           }
//Checking if there is any gain                           
                            if(nB == nowB)
                            b = true;
                            if(hits == 4 && b == true)
                            {
                            netGain = bet;
                            multiplier = 1;
                            stats[1] ++;
                            }
                            else if(hits == 5 && !b)
                            {
                            multiplier = 2;	
                            netGain = bet * multiplier;
                            stats[2]++;
                            }
                            else if(hits == 5 && b)
                            {
                            multiplier = 4;	
                            netGain = bet * multiplier;
                            stats[3]++;
                            }
                            else if(hits == 6 && !b)
                            {
                            multiplier = 8;
                            netGain = bet * multiplier;
                            stats[4]++;
                            }
                            else if(hits == 6 && b)
                            {
                            multiplier = 24;
                            netGain = bet * multiplier;
                            stats[5]++;
                            }
                            else if(hits == 7 && !b)
                            {
                            multiplier = 50;
                            netGain = bet * multiplier;
                            stats[6]++;
                            }
                            else if(hits == 7 && b)
                            {
                            multiplier = 150;
                            netGain = bet * multiplier;
                            stats[7]++;
                            }
                            else if(hits == 8 && !b)
                            {
                            multiplier = 1000;
                            netGain = bet * multiplier;
                            stats[8]++;
                            }
                            else if(hits == 8 && b)
                            {
                            netGain = bet * 10000;
                            multiplier = 10000;
                            stats[9]++;
                            }
                            else
                            stats[0]++;
                            all++;
                            if(playing)
                            profit = profit + netGain;
                            Console.WriteLine("------------------------------------------------");
                            Console.Write("Találatok: {0} | ", hits);
                            Console.WriteLine("B mező: {0}", b? "eltalálva" : "nincs");
                            Console.WriteLine("Nyereményed "+multiplier+"-{0}", multiplier == 8 || multiplier == 0? "szoros." : "szeres.");
                            if(playing)
                            Console.WriteLine("Jelenlegi profit: {0} Ft.", profit);
                            Console.WriteLine("A folytatáshoz nyomj entert.");
                            if(games != 1 && doingSimulation)
                            {
                            games--;
                            goto SimBelep;
                            }
                            if(games == 1 && doingSimulation)
                            doingSimulation = false;
                            string most = "";
                            most = Console.ReadLine();
                            if(most.ToLower() == "asd")
                                 playing = true;
                            else if(most.ToLower() == "stats")
                            {
                            Console.WriteLine("=======[Statisztika]=======\nJátékok száma: {0}\nNem nyert: {1}\n4+1: {2}\n5+0: {3}\n5+1: {4}\n6+0: {5}\n6+1: {6}\n7+0: {7}\n7+1: {8}\n8+0: {9}\n8+1: {10}", all, stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], stats[6], stats[7], stats[8], stats[9]);
                            Console.ReadKey(true);
                            }
                            else if(most.ToLower() == "sim")
                            {
                            Console.WriteLine("Hány játék?");
                            games = uint.Parse(Console.ReadLine());
                            doingSimulation = true;
                            goto SimBelep;
                            }
                            goto start;
                    }
            }
    }

